#ifndef __LOG_HPP__
#define __LOG_HPP__

#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <cstdarg>

#define DEBUG   0
#define NORMAL  1
#define WARNING 2
#define ERROR   3
#define FATAL   4

#define LOGFILE "./thread_pool.log"

const char* gLevelMap[]={
    "DEBUG",
    "NORMAL",
    "WARNING",
    "ERROR",
    "FATAL"
};


//完整的日志功能，至少：日志等级  时间  支持用户自定义（日志内容，文件行，文件名）
void logMessage(int level,const char* format,...)
{
#ifndef DEBUG_SHOW
    if(level==DEBUG) return;
#endif
    //标准部分
    char stdBuff[1024];
    memset(stdBuff,0,sizeof(stdBuff));
    time_t tm=time(nullptr);
    snprintf(stdBuff,sizeof(stdBuff),"[%s] [%d] ",gLevelMap[level],tm);

    //自定义部分
    char logBuff[1024];
    memset(logBuff,0,sizeof(logBuff));
    va_list args;
    va_start(args,format);
    vsnprintf(logBuff,sizeof(logBuff),format,args);
    va_end(args);

#ifdef PRINT
    //往显示器上显示
    printf("%s%s\n",stdBuff,logBuff);
#else
    //将日志信息写入打开的文件中
    FILE* fp=fopen(LOGFILE,"a");
    if(fp==nullptr)
    {
        perror("打开文件");
    }
    fprintf(fp,"%s%s\n",stdBuff,logBuff);
    fclose(fp);
#endif
}

#endif